﻿using System.Drawing;
using TLD_Vision;

namespace TLD_NN {
    public class MapUtility {
        private const int ENLARGEMENT_FACTOR = 5;
        /// <summary>
        /// Maps the weighted map in a neuron to a bitmap.
        /// </summary>
        /// <param name="s">Input neuron size</param>
        /// <param name="n">Neuron containing the weighted map</param>
        /// <returns></returns>
        public static TLD_Vision.Image SaveWeightMapImage(Size s, Neuron n) {
            ImageData imgData = new ImageData(s.Width * ENLARGEMENT_FACTOR, s.Height * ENLARGEMENT_FACTOR);

            int blockSize = ENLARGEMENT_FACTOR * ENLARGEMENT_FACTOR;

            for (int j = 0; j < n.Weights.Length; ++j) {
                // Maps -0.5 - 0.5 to 0 - 255
                int value = (int)((n.Weights[j] + 0.5) * 255);

                // The output is enlarged by factor ENLARGEMENT_FACTOR
                for (int i = 0; i < blockSize; i++) {
                    imgData[
                        (j % s.Width) * ENLARGEMENT_FACTOR
                        + (j / s.Width) * s.Height * blockSize
                        + (i % ENLARGEMENT_FACTOR)
                        + (i / ENLARGEMENT_FACTOR) * s.Height * ENLARGEMENT_FACTOR
                        ] = 0xFF << 24 | value << 16 | value << 8 | value;
                }
            }
            return imgData.ToImage();
        }
    }
}
